﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using DesignerHelper.Addons;
using DesignerHelper.Main;
using SpreadsheetGear;
using SpreadsheetGear.Windows.Forms;
using WorkbookDesigner.Addons.DefineName;

namespace WorkbookDesigner.Addons.WorkbookExplorer
{
    [EditorBrowsable(EditorBrowsableState.Never)]
    public class BaseTabFactory
    {
        private WbBusiness WbInit { get; set; }
        private WsBusiness WsInit { get; set; }

        public BaseTabFactory()
        {
        }

        protected virtual BaseTab InitedTab { get; set; }

        public BaseTab GetBaseTab()
        {
            return InitedTab;
        }

        public void Init(WorkbookView wbv)
        {
            WbInit = new WbBusiness();
            WbInit.TriggerInit(wbv);
            InitedTab = WbInit.InitedTab;
        }

        public void ForWorksheetNode(WorkbookView wbv)
        {
            WsInit = new WsBusiness();
            WsInit.TriggerInit(wbv);
            InitedTab = WsInit.InitedTab;
        }

        public void Init(IWorksheet worksheet)
        {
            WsInit = new WsBusiness();
            WsInit.TriggerInit(worksheet);
            InitedTab = WsInit.InitedTab;
        }

        protected virtual void CheckButtonPanel()
        {
            InitedTab.HideButtonPanel(new[] {0});
        }

        protected virtual void ClearAndAddTabPage(TabPage tab, Control control)
        {
            tab.Controls.Clear();
            tab.Controls.Add(control);
        }

        protected TabPage GetTabPage(string text, string name, Control control)
        {
            TabPage tp = new TabPage("定义名称") {Name = "NamesInWorkSheet"};
            ClearAndAddTabPage(tp, control);
            return tp;
        }

        private class WbBusiness : BaseTabFactory
        {
            private IMarine<IWorkbook> WbOptions { get; }
            private DefineNameView DfNameView { get; }
            //protected sealed override BaseTab InitedTab { get; set; }
            private IWorkbook WorkOn { get; set; }
            private LockBookWorker LbwTrigger { get; set; }

            internal WbBusiness() : base()
            {
                WbOptions = new WorkbookOptions() {Dock = DockStyle.Fill};
                DfNameView = new DefineNameView() {Dock = DockStyle.Fill};
                InitTab();
            }

            internal void TriggerInit(WorkbookView wbv)
            {
                WorkOn = wbv.ActiveWorkbook;
                LbwTrigger = LockBookWorker.GetWorker(WorkOn);
                LbwTrigger.DoWork(() => WbOptions.TriggerInit(WorkOn));
                DfNameView.TriggerInit(WorkOn);
            }

            private void InitTab()
            {
                InitedTab = new BaseTab() {Dock = DockStyle.Fill};
                InitedTab.SetTabPage(GetPagesArray());
                HookEvents();
                if (InitedTab.BaseTc.SelectedIndex == 0)
                    CheckButtonPanel();
            }

            #region Hook&Unhook Events

            private void HookEvents()
            {
                InitedTab.ResetRequest += ResetEventsTrigger;
                InitedTab.SettingRequest += SetEventsTrigger;
                InitedTab.BaseTc.SelectedIndexChanged += TransformButtonPanel;
            }

            private void TransformButtonPanel(object sender, EventArgs e)
            {
                CheckButtonPanel();
            }

            private void UnhookEvents()
            {
                InitedTab.ResetRequest -= ResetEventsTrigger;
                InitedTab.SettingRequest -= SetEventsTrigger;
                InitedTab.BaseTc.SelectedIndexChanged -= TransformButtonPanel;
            }

            private void ResetEventsTrigger(object sender, EventArgs e)
            {
                LbwTrigger.DoWork(WbOptions.TriggerReset);
            }

            private void SetEventsTrigger(object sender, EventArgs e)
            {
                LbwTrigger.DoWork(WbOptions.TriggerSet);
            }

            #endregion

            #region Used tabpage[]

            private TabPage[] GetPagesArray()
            {
                TabPage[] pagesForWorkbookOptions = {GetTpNames(), GetTpOptions()};
                return pagesForWorkbookOptions;
            }

            private TabPage GetTpNames()
            {
                TabPage tp = new TabPage("定义名称") {Name = "DefineName", AutoScroll = true};
                ClearAndAddTabPage(tp, DfNameView);
                return tp;
            }

            private TabPage GetTpOptions()
            {
                TabPage tpOptions = new TabPage("工作簿选项") {Name = "Options", AutoScroll = true};
                ClearAndAddTabPage(tpOptions, WbOptions.GetControl());
                return tpOptions;
            }

            #endregion
        }

        private class WsBusiness : BaseTabFactory
        {
            private PageSetupContainer Psc { get; }
            private IMarine<IOutline> Ol { get; }
            private DefineNameView DfNameView { get; }
            private IMarine<IWorksheet> WsOptions { get; }
            private IWorksheet WorkOn { get; set; }
            private LockBookWorker LbwTrigger { get; set; }

            internal WsBusiness()
            {
                Psc = new PageSetupContainer() {Dock = DockStyle.Fill};
                DfNameView = new DefineNameView() {Dock = DockStyle.Fill};
                WsOptions = new WorksheetOptions() {Dock = DockStyle.Fill};
                Ol = new Outline() {Dock = DockStyle.Fill};
                InitTab();
            }

            internal void TriggerInit(WorkbookView wbv)
            {
                wbv.GetLock();

                WorkOn = wbv.ActiveWorkbook.Worksheets[1]; //bug if更改的工作表不为当前工作表?
                LbwTrigger = LockBookWorker.GetWorker(WorkOn);
                Ol.TriggerInit(WorkOn.Outline);
                WsOptions.TriggerInit(WorkOn);
                Psc.TriggerInit(WorkOn);
                DfNameView.TriggerInit(WorkOn);
                wbv.ReleaseLock();
            }

            internal void TriggerInit(IWorksheet sheet)
            {
                WorkOn = sheet; //bug if更改的工作表不为当前工作表?
                LbwTrigger = LockBookWorker.GetWorker(WorkOn);
                LbwTrigger.DoWork(() =>
                {
                    Ol.TriggerInit(WorkOn.Outline);
                    Psc.TriggerInit(WorkOn);
                    DfNameView.TriggerInit(WorkOn);
                    WsOptions.TriggerInit(WorkOn);
                });
            }

            private void InitTab()
            {
                InitedTab = new BaseTab() {Dock = DockStyle.Fill};
                InitedTab.SetTabPage(PagesForWorksheetOptions());
                HookEvents();

                if (InitedTab.BaseTc.SelectedIndex == 0)
                    CheckButtonPanel();
            }

            #region Hook&Unhook Events

            private void HookEvents()
            {
                InitedTab.SettingRequest += SettingEventsTrigger;
                InitedTab.ResetRequest += ResetEventsTrigger;
                InitedTab.BaseTc.SelectedIndexChanged += TransformButtonPanel;
            }

            private void UnhookEvents()
            {
                InitedTab.SettingRequest -= SettingEventsTrigger;
                InitedTab.ResetRequest -= ResetEventsTrigger;
                InitedTab.BaseTc.SelectedIndexChanged -= TransformButtonPanel;
            }

            private void TransformButtonPanel(object sender, EventArgs e)
            {
                CheckButtonPanel();
            }

            private void SettingEventsTrigger(object sender, EventArgs e)
            {
                Psc.OnSettingCommit();
                LbwTrigger.DoWork(() =>
                {
                    WsOptions.TriggerSet();
                    Ol.TriggerSet();
                });
            }

            private void ResetEventsTrigger(object sender, EventArgs e)
            {
                Psc.OnResetCommit();
                LbwTrigger.DoWork(() =>
                {
                    Ol.TriggerReset();
                    WsOptions.TriggerReset();
                });
            }

            #endregion

            #region used tabpage[]

            private TabPage[] PagesForWorksheetOptions()
            {
                TabPage[] array = {DefineNames(), SheetOptions(), Outline(), PageSetup()};
                return array;
            }

            private TabPage DefineNames()
            {
                TabPage tp = new TabPage("定义名称") {Name = "NamesInWorkSheet"};
                ClearAndAddTabPage(tp, DfNameView as UserControl);
                return tp;
            }

            private TabPage SheetOptions()
            {
                TabPage tp = new TabPage("工作表选项") {Name = "SheetOptions"};
                ClearAndAddTabPage(tp, WsOptions.GetControl());
                return tp;
            }

            private TabPage Outline()
            {
                TabPage tp = new TabPage("分级显示") {Name = "Outline"};
                ClearAndAddTabPage(tp, Ol as UserControl);
                return tp;
            }

            private TabPage PageSetup()
            {
                TabPage tp = new TabPage("页面设置") {Name = "PageSetup"};
                ClearAndAddTabPage(tp, Psc);
                return tp;
            }

            #endregion
        }
    }
}